package com.ruoyi.asset.homepage.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.ruoyi.asset.homepage.domain.bo.AssetOverviewBo;
import com.ruoyi.asset.homepage.service.IHomePageService;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.erecord.region.service.IRegionService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/asset/homepage")
public class HomePageController extends BaseController {

	private final IHomePageService homePageService;
	private final IRegionService regionService;

	/**
	 * 首页-查询资产概况
	 */
	@SaIgnore()
	@RepeatSubmit(interval = 2000)
	@GetMapping(value = "/queryAssetOverview")
	public R<AssetOverviewBo> queryAssetOverview() {
		try {
			AssetOverviewBo assetOverviewBo = this.homePageService.queryAssetOverview();

			return R.ok(assetOverviewBo);
		} catch (Exception e) {
			return R.fail(e.getMessage());
		}
	}

    /**
     * 智慧大屏首页底部百分比概况
     * @param date
     * @return
     */
    @SaIgnore()
    @GetMapping(value = "/queryAssetStatistics")
    public R<JSONArray> queryAssetStatistics(@RequestParam("date") String date) {
        date = "2024-03-13";
        try {
            JSONArray jsonArray = this.homePageService.queryStatistics(date);
            return R.ok(jsonArray);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 智慧大屏管理一张图
     * @param date
     * @return
     */
    @SaIgnore()
    @GetMapping(value = "/queryAssetManageStat")
    public R<JSONObject> queryAssetManageStat(@RequestParam("date") String date, String community) {
        date = "2024-03-13";
        try {
            if (StringUtils.isNotEmpty(community)) {
                String regionIdByName = regionService.getRegionIdByName(community);
                if (StringUtils.isEmpty(regionIdByName)) {
                    return R.fail("查询的社区不存在！");
                }
            }
            JSONObject object = this.homePageService.queryManageStat(date, community);
            return R.ok(object);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }
}
